package net.lastcoder.ifserver.web;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.websocket.api.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * auth: WenYF
 * date: 2017/3/1
 */
public class WebsocketManager {
    private static final Logger Log = LoggerFactory.getLogger(WebsocketManager.class);

    private static class SingletonHolder {
        private static final WebsocketManager instance = new WebsocketManager();
    }

    public static WebsocketManager getInstance() {
        return SingletonHolder.instance;
    }
    
    private Server server;

    public void stopServer() {
        try {
            if (server != null) {
                server.stop();
            }
        } catch (Exception e) {
            Log.error(e.getLocalizedMessage(), e);
        }
    }


    public void startServer() {
        LocalWebSocketHandler handler = new LocalWebSocketHandler();
        server = new Server(10500);
        server.setHandler(handler);
        try {
            server.start();
        } catch (Exception e) {
            Log.error(e.getMessage(), e);
        }
    }
}
